package com.ybb.vue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybb.vue.common.R;
import com.ybb.vue.common.Result;
import com.ybb.vue.common.util.OperationRecordsUtils;
import com.ybb.vue.domain.AlarmTypeEnum;
import com.ybb.vue.entity.EpmAlarmNotes;
import com.ybb.vue.entity.EpmAlarmRule;
import com.ybb.vue.service.EpmAlarmNotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ybb
 * @version 1.0
 * @Description TODO
 * @Date 2023/3/25 22:00
 */

@RestController
@RequestMapping("/alarmNotes")
public class EpmAlarmNotesController {

    @Autowired
    private EpmAlarmNotesService epmAlarmNotesService;

    /**
     * 查询alarmNotes列表
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/alarmNotesList")
    @PreAuthorize("hasAuthority('alarm:management:list')"+"||"+"hasAuthority('alarm:management:nquery')")
    public Result<?> alarmNotesList (@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<EpmAlarmNotes> queryWrapper = Wrappers.<EpmAlarmNotes>lambdaQuery();
        queryWrapper.like(EpmAlarmNotes::getAlarmName, search);
        queryWrapper.orderByDesc(EpmAlarmNotes::getAlarmTime);
        Page<EpmAlarmNotes> result = epmAlarmNotesService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<EpmAlarmNotes> alarmNotesList = result.getRecords();
        alarmNotesList.forEach(alarmNotes -> {
            // 告警类型转译
            alarmNotes.setAlarmType(AlarmTypeEnum.getAlarmType(alarmNotes.getAlarmType()));
        });
        return Result.success(result);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping()
    @PreAuthorize("hasAuthority('alarm:management:ndelete')")
    public R delete(Long id){
        epmAlarmNotesService.removeById(id);

        // 操作记录
        String content = "操作了告警记录表，【删除】了1条记录，记录id为：" + id;
        OperationRecordsUtils.createOperationRecords(content);
        return R.ok();
    }
}
